﻿using System.Web.UI.WebControls.WebParts;
using System.Configuration.Provider;
using FRL.DebtChased.Repository;

namespace FRL.DebtChased.Website
{

	/// <summary>
	/// Declaration of MyWebPartsProvider class
	/// FRL DebtChased-specific implementation of a PersonalizationProvider
	/// </summary>
	public class MyWebPartsProvider : PersonalizationProvider
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public MyWebPartsProvider()
		{

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override string ApplicationName
		{
			get
			{
				throw new System.NotImplementedException();
			}
			set
			{
				throw new System.NotImplementedException();
			}
		}

		public override PersonalizationStateInfoCollection FindState(PersonalizationScope scope, PersonalizationStateQuery query, int pageIndex, int pageSize, out int totalRecords)
		{
			throw new System.NotImplementedException();
		}

		public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
		{
			throw new System.NotImplementedException();
		}

		protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
		{

			// Get the current user-id
			int iUserId = MySession.BindMySession().UserID;

			// Retrieve the shared-data and user-data blob
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{
				Personalization.GetBlobs(ent, path, ref sharedDataBlob, ref userDataBlob, iUserId);
			}
			
		}

		protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)
		{

			// Get the current user-id
			int iUserId = MySession.BindMySession().UserID;

			// Nullify record in database
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{
				Personalization.ResetBlobs(ent, path, iUserId);
			}

		}

		public override int ResetState(PersonalizationScope scope, string[] paths, string[] usernames)
		{
			throw new System.NotImplementedException();
		}

		public override int ResetUserState(string path, System.DateTime userInactiveSinceDate)
		{
			throw new System.NotImplementedException();
		}

		protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] userDataBlob)
		{

			// Get the current user-id
			int iUserId = MySession.BindMySession().UserID;

			// Save the data blob
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{
				Personalization.SetBlobs(ent, path, userDataBlob, iUserId);
			}

		}

	}

}